package com.zfast.config.controller;

import com.zfast.config.constant.SessionKeyConst;
import com.zfast.config.entity.Role;
import com.zfast.config.service.RoleService;
import com.zfast.config.vo.CommonResult;
import com.zfast.config.vo.Data;
import com.zfast.config.vo.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname RoleController
 * @Description None
 * @Date 2019/7/19 16:19
 * @Created by WDD
 */
@Controller
@RequestMapping("/badminton")
public class RoleController {

    @Resource
    private CommonResult commonResult;
    @Resource
    private RoleService roleService;

    @GetMapping("/role")
    public String role() {
        return "badminton/role/roleList";
    }

    /**
     * 异步加载角色列表
     *
     * @param pageno
     * @param pagesize
     * @param rid
     * @return
     */
    @RequestMapping("/roleList")
    @ResponseBody
    public Object roleList(@RequestParam(value = "page", defaultValue = "1") Integer pageno,
                           @RequestParam(value = "limit", defaultValue = "5") Integer pagesize,
                           String rid) {
        Map<String, Object> paramMap = new HashMap();
        paramMap.put("pageno", pageno);
        paramMap.put("pagesize", pagesize);

        //判断是否为空
        if (!StringUtils.isEmpty(rid) && !rid.equals("0")) paramMap.put("rid", rid);

        PageBean<Role> pageBean = roleService.queryPage(paramMap);

        Map<String, Object> rest = new HashMap();
        rest.put("code", 0);
        rest.put("msg", "");
        rest.put("count", pageBean.getTotalsize());
        rest.put("data", pageBean.getDatas());
        return rest;
    }


    /**
     * 删除角色
     *
     * @param data
     * @return
     */
    @PostMapping("/delRole")
    @ResponseBody
    public CommonResult delRole(Data data) {
        int count = roleService.delByRoleIds(data.getIds());
        if (count >= data.getIds().size()) {
            commonResult.ajaxTrue("删除成功");
        } else {
            commonResult.ajaxFalse("存在管理员拥有此角色,无法删除");
        }
        return commonResult;
    }

    /**
     * 跳转添加角色页面
     *
     * @param type
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/addRole")
    public String addRole(String type, String id, Model model) {
        if (type != null && type.equals("edit")) {
            Role role = roleService.selectById(id);
            model.addAttribute(SessionKeyConst.ROLE, role);
        }
        return "badminton/role/addRole";
    }


    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    @ResponseBody
    public CommonResult submitAddRole(Role role) {
        Role byName = roleService.selectByName(role.getName());
        if (role.getId() != null) {
            //修改角色
            if (byName != null && !byName.getId().equals(role.getId())) {
                //与修改角色一样，但存在数据库中，表示后来改的角色已存在
                commonResult.ajaxFalse("角色已存在");
                return commonResult;
            }
            int count = roleService.editByRole(role);
            if (count > 0) {
                commonResult.ajaxTrue("修改成功");
            } else {
                commonResult.ajaxFalse("修改失败");
            }
        } else {
            //添加角色
            if (byName != null) {
                //与原角色不一样，但存在数据库中，表示后来改的角色已存在
                commonResult.ajaxFalse("角色已存在");
                return commonResult;
            }
            int count = roleService.insertRole(role);
            if (count > 0) {
                commonResult.ajaxTrue("添加成功");
            } else {
                commonResult.ajaxFalse("添加失败");
            }
        }
        return commonResult;
    }

    @GetMapping("/allotPer")
    public String allotPer(Integer id, Model model) {
        model.addAttribute("id", id);
        return "/badminton/role/allotPer";
    }
}
